package managedbean;

import entity.Client;
import entity.IndicativeProductPrice;
import entity.PurchaseOrder;
import entity.PurchaseOrderItem;
import entity.Quotation;
import entity.QuotationItem;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import session.IndicativeProductPriceManager;
import session.PurchaseOrderManager;
import session.QuotationManager;
import session.SalesInquiryManager;

@ManagedBean
@SessionScoped
public class PurchaseOrderBean {

    @EJB
    private PurchaseOrderManager purchaseOrderManager;
    @EJB
    private QuotationManager quotationManager;
    @EJB
    private SalesInquiryManager salesInquiryManager;
    @EJB
    private IndicativeProductPriceManager ippm;
    private Quotation quotation;
    private Long purchaseOrderId;
    private Long orderId;
    private Long quotationId;
    private String businessType;
    private String productName;
    private Integer numOfProduct;
    private Double productPrice;
    private Timestamp dateSubmitted;
    private String submittedBy;
    private String shippingAddress;
    private Collection<PurchaseOrder> purchaseOrderList = new ArrayList<PurchaseOrder>();
    private Collection<PurchaseOrderItem> purchaseOrderItems = new ArrayList<PurchaseOrderItem>();
    private Collection<IndicativeProductPrice> productList = new ArrayList<IndicativeProductPrice>();
    private Collection<Quotation> quotationList = new ArrayList<Quotation>();
    private Double totalPrice;
    private Long clientId;
    private Collection<Client> clients = new ArrayList<Client>();
    private Collection<QuotationItem> quotationItemList;

    /** Creates a new instance of purchaseOrderBean */
    public PurchaseOrderBean() {
        totalPrice = 0.0;
    }

    public void addNewPurchaseOrder(ActionEvent event) {
//activated when user submits data
        purchaseOrderManager.createPurchaseOrder(quotationId);
        dateSubmitted = purchaseOrderManager.getPurchaseOrder().getDateSubmitted();

        quotation = quotationManager.getQuotation(quotationId);

        for (Object o : quotation.getQuotationItems()) {
            PurchaseOrderItem tempPurchaseOrderItem = new PurchaseOrderItem();
            QuotationItem tempQuotationItem = (QuotationItem) o;
            tempPurchaseOrderItem.setNumOfProduct(tempQuotationItem.getNumOfProduct());
            tempPurchaseOrderItem.setProductName(tempQuotationItem.getProductName());
            tempPurchaseOrderItem.setProductPrice(tempQuotationItem.getProductPrice());
            purchaseOrderItems.add(tempPurchaseOrderItem);
        }
    }

    public void calculateTotalPrice(ActionEvent event) {
        for (Object o : purchaseOrderItems) {
            PurchaseOrderItem tempPurchaseOrderItem = new PurchaseOrderItem();
            tempPurchaseOrderItem = (PurchaseOrderItem) o;
            totalPrice += (tempPurchaseOrderItem.getNumOfProduct() * tempPurchaseOrderItem.getProductPrice());
        }
    }

    public void finalisePurchaseOrder(ActionEvent event) {
        purchaseOrderManager.addPurchaseOrderItem(purchaseOrderItems);
        purchaseOrderManager.persist();
        purchaseOrderId = purchaseOrderManager.getPurchaseOrder().getId();
    }

    public void getCompletedQuotationList() {
        quotationList = quotationManager.getCompletedQuotationList();
    }

    public void getClientCompletedQuotationList() {
        Client c = new Client();

        //COMMENT OFF UNTIL INTEGRATION, UNTESTED CODE
        //String username  = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        // c = quotationManager.getClient(username);

        //to be removed/replaced by part above during INTEGRATION , need to have a client in database with id=43 for this to work
        c = quotationManager.getClient(43L);

        clientId = c.getId();
        quotationList = quotationManager.getClientCompletedQuotationList(clientId);
    }

    public void resetAfterAction(ActionEvent event) {
        resetAll();
    }

    public void resetAll() {
        quotationId = null;
        dateSubmitted = null;;
        submittedBy = null;
        totalPrice = 0.0;
        purchaseOrderList = new ArrayList<PurchaseOrder>();
        purchaseOrderItems = new ArrayList<PurchaseOrderItem>();
        productList = new ArrayList<IndicativeProductPrice>();
        quotationList = new ArrayList<Quotation>();
    }

    public void confirmNewPurchaseOrder(ActionEvent event) {
        getPurchaseOrderManager().persist();
        setPurchaseOrderId(getPurchaseOrderManager().getPurchaseOrder().getId());
    }

    public void matchQuotationPurchaseOrder(ActionEvent event) {
        PurchaseOrder p = purchaseOrderManager.getPurchaseOrder(purchaseOrderId); //get PurchaseOrder
        Quotation q = quotationManager.getQuotation(quotationId); //get Quotation
        purchaseOrderItems = purchaseOrderManager.getPurchaseOrderItemList(p.getId());
        quotationItemList = quotationManager.getQuotationItems(p.getQuotationId());
        purchaseOrderManager.matchQuotationPurchaseOrder(p, q, purchaseOrderItems, quotationItemList);
    }

    //REMOVE IF NO USAGE
    public void getQuotationList(ActionEvent event) {
        quotationList = quotationManager.getQuotationList();
    }

    public void retrievePurchaseOrderList(ActionEvent event) {
        purchaseOrderList = purchaseOrderManager.getPurchaseOrderList();
    }

    public void retrievePurchaseOrderItemList(ActionEvent event) {
        purchaseOrderItems = purchaseOrderManager.getPurchaseOrderItemList(purchaseOrderId);
    }

    public void retrieveClients(ActionEvent event) {
        clients = salesInquiryManager.getClients();
    }
    
        public void getClientPurchaseOrderList(ActionEvent event) {
        //COMMENT OFF UNTIL INTEGRATION, UNTESTED CODE (Samuel's code)
        //String username  = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        // c = quotationManager.getClient(username);

        String user = "SystemUser"; //get username from session
        purchaseOrderList = purchaseOrderManager.getPurchaseOrderList(user);
    }

    public void viewPurchaseOrder(ActionEvent event) {
        PurchaseOrder purchase = purchaseOrderManager.getPurchaseOrder(purchaseOrderId.longValue());
        System.out.println("Got a Purchase Order, submitted by: " + purchase.getSubmittedBy()); // TEST
setPurchaseOrderId(purchase.getId());
setDateSubmitted(purchase.getDateSubmitted());
setTotalPrice(purchase.getTotalPrice());
setSubmittedBy(purchase.getSubmittedBy());
setPurchaseOrderItems(purchase.getPurchaseOrderItems());
//setSalesOrderId(purchase.getSalesOrderId());

        purchaseOrderList.add(purchase);
    }

    public void viewAllPurchaseOrder(ActionEvent event) {
        purchaseOrderList = purchaseOrderManager.getPurchaseOrderList();
    }

    public void resetItem(ActionEvent event) {
        this.setQuotationId(null);
        this.setProductPrice(null);
        this.setNumOfProduct(null);
    }

    public void retrieveProductList(String businessType) {
        productList = ippm.getProductsList(businessType);
    }

    /**
     * @return the purchaseOrderManager
     */
    public PurchaseOrderManager getPurchaseOrderManager() {
        return purchaseOrderManager;
    }

    /**
     * @param purchaseOrderManager the purchaseOrderManager to set
     */
    public void setPurchaseOrderManager(PurchaseOrderManager purchaseOrderManager) {
        this.purchaseOrderManager = purchaseOrderManager;
    }

    /**
     * @return the purchaseOrderId
     */
    public Long getPurchaseOrderId() {
        return purchaseOrderId;
    }

    /**
     * @param purchaseOrderId the purchaseOrderId to set
     */
    public void setPurchaseOrderId(Long purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    /**
     * @return the productName
     */
    public String getProductName() {
        return productName;
    }

    /**
     * @param productName the productName to set
     */
    public void setProductName(String productName) {
        this.productName = productName;
    }

    /**
     * @return the numOfProduct
     */
    public Integer getNumOfProduct() {
        return numOfProduct;
    }

    /**
     * @param numOfProduct the numOfProduct to set
     */
    public void setNumOfProduct(Integer numOfProduct) {
        this.numOfProduct = numOfProduct;
    }

    /**
     * @return the productPrice
     */
    public Double getProductPrice() {
        return productPrice;
    }

    /**
     * @param productPrice the productPrice to set
     */
    public void setProductPrice(Double productPrice) {
        this.productPrice = productPrice;
    }

    /**
     * @return the dateSubmitted
     */
    public Timestamp getDateSubmitted() {
        return dateSubmitted;
    }

    /**
     * @param dateSubmitted the dateSubmitted to set
     */
    public void setDateSubmitted(Timestamp dateSubmitted) {
        this.dateSubmitted = dateSubmitted;
    }

    /**
     * @return the submittedBy
     */
    public String getSumbittedBy() {
        return submittedBy;
    }

    /**
     * @param submittedBy the submittedBy to set
     */
    public void setSumbittedBy(String submittedBy) {
        this.submittedBy = submittedBy;
    }

    /**
     * @return the purchaseOrderList
     */
    public Collection<PurchaseOrder> getPurchaseOrderList() {
        return purchaseOrderList;
    }

    /**
     * @param purchaseOrderList the purchaseOrderList to set
     */
    public void setPurchaseOrderList(Collection<PurchaseOrder> purchaseOrderList) {
        this.purchaseOrderList = purchaseOrderList;
    }

    /**
     * @return the purchaseOrderItems
     */
    public Collection<PurchaseOrderItem> getPurchaseOrderItemList() {
        return purchaseOrderItems;
    }

    /**
     * @param purchaseOrderItems the purchaseOrderItems to set
     */
    public void setPurchaseOrderItemList(Collection<PurchaseOrderItem> purchaseOrderItem) {
        this.purchaseOrderItems = purchaseOrderItem;
    }

    public String getShippingAddress() {
        return shippingAddress;
    }

    public void setShippingAddress(String address) {
        shippingAddress = address;
    }

    /**
     * @return the quotationId
     */
    public Long getQuotationId() {
        return quotationId;
    }

    /**
     * @param quotationId the quotationId to set
     */
    public void setQuotationId(Long quotationId) {
        this.quotationId = quotationId;
    }

    /**
     * @return the submittedBy
     */
    public String getSubmittedBy() {
        return submittedBy;
    }

    /**
     * @param submittedBy the submittedBy to set
     */
    public void setSubmittedBy(String submittedBy) {
        this.submittedBy = submittedBy;
    }

    /**
     * @return the productList
     */
    public Collection<IndicativeProductPrice> getProductList() {
        return productList;
    }

    /**
     * @param productList the productList to set
     */
    public void setProductList(Collection<IndicativeProductPrice> productList) {
        this.productList = productList;
    }

    /**
     * @return the orderId
     */
    public Long getOrderId() {
        return orderId;
    }

    /**
     * @param orderId the orderId to set. And set purchase order id to this.
     */
    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public QuotationManager getQuotationManager() {
        return quotationManager;
    }

    public void setQuotationManager(QuotationManager quotationManager) {
        this.quotationManager = quotationManager;
    }

    public Collection<Quotation> getQuotationList() {
        return quotationList;
    }

    public void setQuotationList(Collection<Quotation> quotationList) {
        this.quotationList = quotationList;
    }

    /**
     * @param purchaseOrderItems the purchaseOrderItems to set
     */
    public void setPurchaseOrderItems(Collection<PurchaseOrderItem> purchaseOrderItems) {
        this.purchaseOrderItems = purchaseOrderItems;
    }

    /**
     * @return the purchaseOrderItems
     */
    public Collection<PurchaseOrderItem> getPurchaseOrderItems() {
        return purchaseOrderItems;
    }

    public SalesInquiryManager getSalesInquiryManager() {
        return salesInquiryManager;
    }

    public void setSalesInquiryManager(SalesInquiryManager salesInquiryManager) {
        this.salesInquiryManager = salesInquiryManager;
    }

    public IndicativeProductPriceManager getIppm() {
        return ippm;
    }

    public void setIppm(IndicativeProductPriceManager ippm) {
        this.ippm = ippm;
    }

    public Long getClientId() {
        return clientId;
    }

    public void setClientId(Long clientId) {
        this.clientId = clientId;
    }

    public String getBusinessType() {
        return businessType;
    }

    public void setBusinessType(String businessType) {
        this.businessType = businessType;
    }

    public Collection<Client> getClients() {
        return clients;
    }

    public void setClients(Collection<Client> clients) {
        this.clients = clients;
    }

    public Collection<QuotationItem> getQuotationItemList() {
        return quotationItemList;
    }

    public void setQuotationItemList(Collection<QuotationItem> quotationItemList) {
        this.quotationItemList = quotationItemList;
    }

    public Double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(Double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Quotation getQuotation() {
        return quotation;
    }

    public void setQuotation(Quotation quotation) {
        this.quotation = quotation;
    }
}
